<template>
	<view class="indexs">
		<view class="header header_wi" :style="'padding-top: ' + navH + 'px;'">
			<view class="header_height" :style="'height:' + gaodu + 'px;line-height:' + gaodu + 'px;top:' + gaodutops + 'px;'">
				<view class="index_h_cont3">
					<view class="index_wenben_img" @tap="jumpzuo"><image :src="imgUrl + '/dzx_img33.png'" mode="widthFix"></image></view>
					<view class="index_wenben">{{ $t('page_shangpinpinlei.commodity_category') }}</view>
				</view>
			</view>
			<view class="coloe_title">s</view>
		</view>
		<view class="index_center" :style="'padding-top:' + (navH + 50) + 'px;padding-bottom: 150rpx;'">
			<view class="shangpinpinlei_list">
				<view class="shangpinpinlei_cont" v-for="(item, indexs) in shujulister" :key="indexs">
					<view class="shangpinpinlei_title">{{ item.name }}</view>

					<view class="shangpinpinlei_cont_list">
						<block v-for="(items, index) in item.child" :key="index">
							<view
								:class="'shangpinpinlei_cont_list_cont ' + (items.is_show ? 'ons' : '')"
								@tap="btnson"
								:data-name="item.name"
								:data-id="items.category_id"
								:data-index2="index"
								:data-index="indexs"
							>
								{{ items.name }}
							</view>
						</block>
					</view>
				</view>
			</view>

			<view class="shangpinpinlei_btns">
				<view class="shangpinpinlei_btns_dis">
					<view class="shangpinpinlei_btns1" @tap="chongzhi">{{ $t('page_shangpinpinlei.reset') }}</view>
					<view class="shangpinpinlei_btns2" @tap="queren">{{ $t('page_shangpinpinlei.confirm') }}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
// pages/shouye/shouye_zkc/shouye_zkc.js
const app = getApp();

let request = require('../../../utils/require');

export default {
	data() {
		return {
			navH: app.globalData.navHeight,
			gaodu: app.globalData.height,
			gaodutops: app.globalData.top,
			imgUrl: app.globalData.imgUrl,
			currid: null,
			shujulist: [],
			cunruid: [],
			shujulister: [],
			shujulister2: [],
			names: '',
			ids: ''
		};
	},
	/**
	 * 生命周期函数--监听页面加载
	 */ onLoad() {
		var that = this;
		that.shujulist = that.shujulister
		that.shangpinpinlei();
	},
	onShow() {
		var that = this;
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {},
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {},
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {},
	methods: {
		shangpinpinlei() {
			var that = this;
			var data = {};
			request.get(`index.php?s=/api/package/category&wxapp_id=` + uni.getStorageSync('web_id'), data).then(res => {
				if (res.data.code == 1) {
					this.shujulister = res.data.data
					console.log(res.data, 're?????');
				}
			});
		},

		queren() {
			if (this.cunruid.length != 0) {
				var names = [];
				var ids = [];

				for (var i = 0; i < this.cunruid.length; i++) {
					if (names.length == 0) {
						names.push(this.cunruid[i].name);
					} else {
						var a = names.indexOf(this.cunruid[i].name);

						if (a > -1) {
						} else {
							names.push(this.cunruid[i].name);
						}
					}

					ids.push(this.cunruid[i].id);
				}

				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2];
				prevPage.names = names.toString(),
				prevPage.ids = ids.toString()
				uni.navigateBack({
				    delta: 1
				});
			}
		},

		chongzhi() {
			var that = this;
			that.cunruid = []
			that.shujulister = []
			that.shangpinpinlei();
		},

		btnson(e) {
			var shujulists = this.shujulister;
			var cunruids = this.cunruid;
			console.log(e, '????');

			if (cunruids.length == 0) {
				var items = {
					id: e.currentTarget.dataset.id,
					name: e.currentTarget.dataset.name
				};
				shujulists[e.currentTarget.dataset.index].child[e.currentTarget.dataset.index2].is_show = true;
				this.cunruid = cunruids.concat(items)
				this.shujulister = shujulists
			} else {
				for (var i = 0; i < cunruids.length; i++) {
					if (cunruids[i].id == e.currentTarget.dataset.id) {
						cunruids.splice(i, 1);
						shujulists[e.currentTarget.dataset.index].child[e.currentTarget.dataset.index2].is_show = false;
						this.cunruid = cunruids
					} else {
						var items = {
							id: e.currentTarget.dataset.id,
							name: e.currentTarget.dataset.name
						};
						this.cunruid = cunruids.concat(items)
						shujulists[e.currentTarget.dataset.index].child[e.currentTarget.dataset.index2].is_show = true;
					}
				}
			}

			this.shujulister = shujulists
		},

		jumpzuo() {
			uni.navigateBack();
		}
	}
};
</script>
<style>
@import './shangpinpinlei.css';
</style>
